//
//  TTAddAddressViewCell.m
//  TaoTuo
//
//  Created by 周晨晔 on 2019/9/21.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTAddAddressViewCell.h"
#import "AddressView.h"
#import "SPModalView.h"

@interface TTAddAddressViewCell ()

@property (nonatomic, strong) TTAddAddressModel *model;
@property (nonatomic, strong) AddressView *addressView;
@property (nonatomic, strong) SPModalView *modalView;

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UITextField *contentTF;
@property (weak, nonatomic) IBOutlet UIButton *upBtn;

@end

@implementation TTAddAddressViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.backgroundColor = [UIColor whiteColor];
    self.upBtn.titleLabel.numberOfLines = 2;
    self.upBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    
    @weakify(self)
    [self.contentTF.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        @strongify(self)
        self.model.content = x;
    }];
    
}


- (IBAction)clickUpButton:(UIButton *)sender {
    
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    
    [self.modalView show];
    
}

- (void)setModel:(TTAddAddressModel *)model areaArray:(NSArray *)areaArray
{
    self.model = model;
    
    self.titleLabel.text = model.title;
    
    self.upBtn.hidden = !model.canClick;
    
    self.contentTF.keyboardType = model.keyboardType;
    self.contentTF.enabled = !model.canClick;
     
    if (model.canClick) {
        self.addressView.datas = areaArray;
        [self.upBtn setTitle:model.content forState:UIControlStateNormal];
        self.contentTF.text = @"";
        self.contentTF.placeholder = model.content.length > 0 ? @"" : model.placeholder;
    } else {
        [self.upBtn setTitle:@"" forState:UIControlStateNormal];
        self.contentTF.text = model.content;
        self.contentTF.placeholder = model.placeholder;
    }
    
}

- (SPModalView *)modalView
{
    if (!_modalView) {
        _modalView = [[SPModalView alloc] initWithView:self.addressView inBaseViewController:self.viewController];
        _modalView.narrowedOff = YES;
    }
    return _modalView;
}

- (AddressView *)addressView {
    
    if (!_addressView) {
        _addressView = [[AddressView alloc] init];
        _addressView.frame = CGRectMake(0, 0, kScreenWidth, 400);
        _addressView.topLabel.text = @"请选择收货地址";
        // 最后一列的行被点击的回调
        @weakify(self)
        _addressView.lastComponentClickedBlock = ^(SPProvince *selectedProvince, SPCity *selectedCity, SPDistrict *selectedDistrict) {
            @strongify(self)
            
            [self.modalView hide];
            
            self.contentTF.placeholder = @"";
            
            NSString *areaString = [NSString stringWithFormat:@"%@ %@",selectedProvince.fullname,selectedCity.fullname];
            
            if (selectedDistrict.fullname.length > 0) {
                areaString = [NSString stringWithFormat:@"%@ %@", areaString, selectedDistrict.fullname];
            }
            
            [self.upBtn setTitle:areaString forState:UIControlStateNormal];
            self.model.content = areaString;
            
        };
    }
    return _addressView;
}


@end
